package yb.ecp.fast.user.service;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.user.dao.entity.CustomerDO;
import yb.ecp.fast.user.dao.mapper.CustomerMapper;
import yb.ecp.fast.user.exception.AuthException;
import yb.ecp.fast.user.service.auth.handler.CustomerLoginErrorEnum;

import java.util.Date;


/**
 * Customer 后台接口实现类
 */
@Service
public class CustomerService {

        @Autowired
        private CustomerMapper customerMapper;

        @Autowired
        private FastGenClient fastGenClient;
        private final Logger log = LoggerFactory.getLogger(this.getClass());


        public CustomerDO getByOpenId(String orgId, String openId, String mobilePhone, String unionId) {
                if (null == openId) {
                        throw new AuthException("参数错误");
                }
                CustomerDO r = null;
                if (StringUtils.isNoneBlank(mobilePhone)) {
                        r = customerMapper.getByMobilePhone(orgId, mobilePhone);
                        if (null == r) {
                                //未查询到 进行新增
                                r = new CustomerDO();
                                //先这样
                                r.setId(fastGenClient.numberGuid().getValue());
                                r.setCname(mobilePhone);
                                //todo 需求小程序确定是哪个orgid
                                r.setOrgId(orgId);
                                r.setOrgName("");
                                r.setMobilePhone(mobilePhone);
                                r.setOpenId(openId);
                                r.setUnionId(unionId);
                                r.setLastLoginTime(new Date());
                                r.setStatus(1);
                                r.setSource(1);
                                r.setCreatedTime(new Date());
                                if (!(customerMapper.insert(r)>0)) {
                                        throw new AuthException(CustomerLoginErrorEnum.wxMobileInsertError.getCode(), CustomerLoginErrorEnum.wxMobileInsertError.getDescription());
                                }
                        }
                        if (Integer.valueOf(0).equals(r.getStatus())) {
                                throw new AuthException(CustomerLoginErrorEnum.customerDisableError.getCode(), CustomerLoginErrorEnum.customerDisableError.getDescription());
                        }
                        if (StringUtils.isNoneBlank(mobilePhone) && null != r.getMobilePhone() && !r.getMobilePhone().trim().equals(mobilePhone)) {
                                throw new AuthException(CustomerLoginErrorEnum.wxMobileBindedError.getCode(), CustomerLoginErrorEnum.wxMobileBindedError.getDescription());
                        }
                        //将openid与其绑定
                        if (StringUtils.isNoneBlank(r.getOpenId())
                                && StringUtils.isNotBlank(openId)
                                && !openId.equals(r.getOpenId())) {
                                log.info("该手机号码已绑定其他微信,{}", r);
                                throw new AuthException(CustomerLoginErrorEnum.mobileBindError.getCode(), CustomerLoginErrorEnum.mobileBindError.getDescription());
                        }
                        if (StringUtils.isNoneBlank(openId)) {
                                CustomerDO customerDO = new CustomerDO();
                                customerDO.setId(r.getId());
                                customerDO.setOpenId(openId);
                                customerDO.setUnionId(unionId);
                                customerDO.setLastLoginTime(new Date());
                                boolean result = customerMapper.updateByPrimaryKeySelective(customerDO) > 0;
                                if (result) {
                                        r.setOpenId(openId);
                                        return r;
                                }
                        }
                        return r;
                }
                if (StringUtils.isNoneBlank(openId)) {
                        r = customerMapper.getByopenId(orgId, openId);
                        if (null != r) {
                                if (Integer.valueOf(0).equals(r.getStatus())) {
                                        throw new AuthException(CustomerLoginErrorEnum.customerDisableError.getCode(), CustomerLoginErrorEnum.customerDisableError.getDescription());
                                }
                                if (StringUtils.isNoneBlank(mobilePhone) && null != r.getMobilePhone() && !r.getMobilePhone().trim().equals(mobilePhone)) {
                                        throw new AuthException(CustomerLoginErrorEnum.wxMobileBindedError.getCode(), CustomerLoginErrorEnum.wxMobileBindedError.getDescription());
                                }
                                //将openid与其绑定
                                return r;
                        }
                }
                throw new AuthException(CustomerLoginErrorEnum.wxUnBindedError.getCode(), CustomerLoginErrorEnum.wxUnBindedError.getDescription());
        }

}
